home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIFCLRMP.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  10KB  |  280 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to modify GIF file color map(s). Images are not modified at all.   *
  7. * Options:                                     *
  8. * -q : quite printing mode.                             *
  9. * -s : save screen color map (unless -i - see below), to stdout.         *
  10. * -l colormap.file : substitute given colormap.file colormap into screen     *
  11. *    colormap (unless -i - see below).                         *
  12. * -g correction : apply gamma correction to the screen colormap (unless -i - *
  13. *    see below).                                 *
  14. * -i n : select image number n color map instead of screen map for above     *
  15. *    operations (n = 1 for first image).                     *
  16. * -h : on line help.                                 *
  17. *   All color maps are ascii text files, with one line per color of the      *
  18. * form: index, Red color, Green color, Blue color - all in the range 0..255. *
  19. *   All color maps should be in the exact same length.                 *
  20. ******************************************************************************
  21. * History:                                     *
  22. * 17 Jul 89 - Version 1.0 by Gershon Elber.                     *
  23. *****************************************************************************/
  24.  
  25. #ifdef __MSDOS__
  26. #include <stdlib.h>
  27. #include <alloc.h>
  28. #endif /* __MSDOS__ */
  29.  
  30. #include <math.h>
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include <string.h>
  34. #include "gif_lib.h"
  35. #include "getarg.h"
  36.  
  37. #define PROGRAM_NAME    "GifClrMp"
  38.  
  39. #ifdef __MSDOS__
  40. extern unsigned int
  41.     _stklen = 16384;                 /* Increase default stack size. */
  42. #endif /* __MSDOS__ */
  43.  
  44. #ifdef SYSV
  45. static char *VersionStr =
  46.         "Gif library module,\t\tGershon Elber\n\
  47.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  48. static char
  49.     *CtrlStr = "GifClrMp q%- s%- l%-ColorMapFile!s g%-Gamma!F i%-Image#!d h%- GifFile!*s";
  50. #else
  51. static char
  52.     *VersionStr =
  53.     PROGRAM_NAME
  54.     GIF_LIB_VERSION
  55.     "    Gershon Elber,    "
  56.     __DATE__ ",   " __TIME__ "\n"
  57.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  58. static char
  59.     *CtrlStr =
  60.     PROGRAM_NAME
  61.     " q%- s%- l%-ColorMapFile!s g%-Gamma!F i%-Image#!d h%- GifFile!*s";
  62. #endif /* SYSV */
  63.  
  64. static int
  65.     SaveFlag = FALSE,
  66.     LoadFlag = FALSE,
  67.     GammaFlag = FALSE;
  68. static
  69.     double Gamma = 1.0;
  70. static
  71.     FILE *ColorFile = NULL;
  72.  
  73.  
  74. static void ModifyColorMap(GifColorType *ColorMap, int BitsPerPixel);
  75. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  76.  
  77. /******************************************************************************
  78. * Interpret the command line and scan the given GIF file.              *
  79. ******************************************************************************/
  80. void main(int argc, char **argv)
  81. {
  82.     int    Error, NumFiles, ExtCode, CodeSize, ImageNum = 0,
  83.     ImageNFlag = FALSE, ImageN, HelpFlag = FALSE, HasGIFOutput;
  84.     GifRecordType RecordType;
  85.     GifByteType *Extension, *CodeBlock;
  86.     char **FileName = NULL, *ColorFileName;
  87.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  88.  
  89.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  90.         &SaveFlag, &LoadFlag, &ColorFileName,
  91.         &GammaFlag, &Gamma, &ImageNFlag, &ImageN,
  92.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  93.         (NumFiles > 1 && !HelpFlag)) {
  94.     if (Error)
  95.         GAPrintErrMsg(Error);
  96.     else if (NumFiles > 1)
  97.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  98.     GAPrintHowTo(CtrlStr);
  99.     exit(1);
  100.     }
  101.  
  102.     if (HelpFlag) {
  103.     fprintf(stderr, VersionStr);
  104.     GAPrintHowTo(CtrlStr);
  105.     exit(0);
  106.     }
  107.  
  108.     if (SaveFlag && LoadFlag || SaveFlag && GammaFlag || LoadFlag && GammaFlag)
  109.     GIF_EXIT("Can not handle more than one of -s -l or -g at the same time.");
  110.  
  111.     if (NumFiles == 1) {
  112.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  113.         QuitGifError(GifFileIn, GifFileOut);
  114.     }
  115.     else {
  116.     /* Use the stdin instead: */
  117.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  118.         QuitGifError(GifFileIn, GifFileOut);
  119.     }
  120.  
  121.     if (SaveFlag) {
  122.     /* We are dumping out the color map as text file to stdout: */
  123.     ColorFile = stdout;
  124.     }
  125.     else if (LoadFlag) {
  126.     /* We are loading new color map from specified file: */
  127.     if ((ColorFile = fopen(ColorFileName, "rt")) == NULL)
  128.         GIF_EXIT("Failed to open specified color map file.");
  129.     }
  130.  
  131.     if ((HasGIFOutput = (LoadFlag || GammaFlag)) != 0) {
  132.     /* Open stdout for GIF output file: */
  133.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  134.         QuitGifError(GifFileIn, GifFileOut);
  135.     }
  136.  
  137.     if (!ImageNFlag) {
  138.     /* We are suppose to modify the screen color map, so do it: */
  139.     ModifyColorMap(GifFileIn -> SColorMap, GifFileIn -> SBitsPerPixel);
  140.     if (!HasGIFOutput) {
  141.         /* We can quit here, as we have the color map: */
  142.         if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  143.         fclose(ColorFile);
  144.         exit(0);
  145.     }
  146.     }
  147.     /* And dump out its new possible repositioned screen information: */
  148.     if (HasGIFOutput)
  149.     if (EGifPutScreenDesc(GifFileOut,
  150.         GifFileIn -> SWidth, GifFileIn -> SHeight,
  151.         GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  152.         GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  153.         QuitGifError(GifFileIn, GifFileOut);
  154.  
  155.     /* Scan the content of the GIF file and load the image(s) in: */
  156.     do {
  157.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  158.         QuitGifError(GifFileIn, GifFileOut);
  159.  
  160.     switch (RecordType) {
  161.         case IMAGE_DESC_RECORD_TYPE:
  162.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  163.             QuitGifError(GifFileIn, GifFileOut);
  164.         if (++ImageNum == ImageN && ImageNFlag) {
  165.             /* We are suppose to modify this image color map, do it: */
  166.             ModifyColorMap(GifFileIn -> SColorMap,
  167.                                                   GifFileIn -> SBitsPerPixel);
  168.             if (!HasGIFOutput) {
  169.             /* We can quit here, as we have the color map: */
  170.             if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  171.             fclose(ColorFile);
  172.             exit(0);
  173.             }
  174.         }
  175.         if (HasGIFOutput)
  176.             if (EGifPutImageDesc(GifFileOut,
  177.             GifFileIn -> ILeft, GifFileIn -> ITop,
  178.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  179.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  180.             GifFileIn -> IColorMap) == GIF_ERROR)
  181.             QuitGifError(GifFileIn, GifFileOut);
  182.  
  183.         /* Now read image itself in decoded form as we dont really   */
  184.         /* care what we have there, and this is much faster.         */
  185.         if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR)
  186.             QuitGifError(GifFileIn, GifFileOut);
  187.         if (HasGIFOutput)
  188.             if (EGifPutCode(GifFileOut, CodeSize, CodeBlock) == GIF_ERROR)
  189.             QuitGifError(GifFileIn, GifFileOut);
  190.         while (CodeBlock != NULL) {
  191.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR)
  192.             QuitGifError(GifFileIn, GifFileOut);
  193.             if (HasGIFOutput)
  194.             if (EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
  195.                 QuitGifError(GifFileIn, GifFileOut);
  196.         }
  197.         break;
  198.         case EXTENSION_RECORD_TYPE:
  199.         /* Skip any extension blocks in file: */
  200.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  201.             QuitGifError(GifFileIn, GifFileOut);
  202.         if (HasGIFOutput)
  203.             if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  204.                             Extension) == GIF_ERROR)
  205.             QuitGifError(GifFileIn, GifFileOut);
  206.  
  207.         /* No support to more than one extension blocks, so discard: */
  208.         while (Extension != NULL) {
  209.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  210.             QuitGifError(GifFileIn, GifFileOut);
  211.         }
  212.         break;
  213.         case TERMINATE_RECORD_TYPE:
  214.         break;
  215.         default:            /* Should be traps by DGifGetRecordType. */
  216.         break;
  217.     }
  218.     }
  219.     while (RecordType != TERMINATE_RECORD_TYPE);
  220.  
  221.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  222.     QuitGifError(GifFileIn, GifFileOut);
  223.     if (HasGIFOutput)
  224.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  225.         QuitGifError(GifFileIn, GifFileOut);
  226. }
  227.  
  228. /******************************************************************************
  229. * Modify the given colormap according to global variables setting.          *
  230. ******************************************************************************/
  231. static void ModifyColorMap(GifColorType *ColorMap, int BitsPerPixel)
  232. {
  233.     int i, Dummy, Red, Green, Blue;
  234.     double Gamma1;
  235.  
  236.     if (SaveFlag) {
  237.     /* Save this color map to ColorFile: */
  238.     for (i = 0; i < (1 << BitsPerPixel); i++)
  239.         fprintf(ColorFile, "%3d %3d %3d %3d\n", i,
  240.         ColorMap[i].Red, ColorMap[i].Green, ColorMap[i].Blue);
  241.     }
  242.     else if (LoadFlag) {
  243.     /* Read the color map in ColorFile into this color map: */
  244.     for (i = 0; i < (1 << BitsPerPixel); i++) {
  245.         if (feof(ColorFile))
  246.         GIF_EXIT("Color file to load color map from, too small.");
  247.         fscanf(ColorFile, "%3d %3d %3d %3d\n", &Dummy, &Red, &Green, &Blue);
  248.         ColorMap[i].Red = Red;
  249.         ColorMap[i].Green = Green;
  250.         ColorMap[i].Blue = Blue;
  251.     }
  252.     }
  253.     else if (GammaFlag) {
  254.     /* Apply gamma correction to this color map: */
  255.     Gamma1 = 1.0 / Gamma;
  256.     for (i = 0; i < (1 << BitsPerPixel); i++) {
  257.         ColorMap[i].Red =
  258.         ((int) (255 * pow(ColorMap[i].Red / 255.0, Gamma1)));
  259.         ColorMap[i].Green =
  260.         ((int) (255 * pow(ColorMap[i].Green / 255.0, Gamma1)));
  261.         ColorMap[i].Blue =
  262.         ((int) (255 * pow(ColorMap[i].Blue / 255.0, Gamma1)));
  263.     }
  264.     }
  265.     else
  266.     GIF_EXIT("Nothing to do!");
  267. }
  268.  
  269. /******************************************************************************
  270. * Close both input and output file (if open), and exit.                  *
  271. ******************************************************************************/
  272. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  273. {
  274.     PrintGifError();
  275.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  276.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  277.     exit(1);
  278. }
  279.  
  280.